public class Solution1190 {
    public String reverseParentheses(String s) {
        int count=0;
        int start=-1;
        StringBuilder stringBuilder=new StringBuilder();
        for (int i=0;i<s.length();i++){
            if (s.charAt(i)=='('){
                if (count==0){
                    start=i;
                }
                count++;
            }else if (s.charAt(i)==')') {
                count--;
                if (count==0){
                    stringBuilder.append(new StringBuilder(reverseParentheses(s.substring(start + 1, i))).reverse());
                }
            }else{
                if (count==0){
                    stringBuilder.append(s.charAt(i));
                }
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution1190().reverseParentheses("(u(love)i)"));
    }
}
